package com.fightingFish.user.servlet;

import com.fightingFish.user.bean.Administrator;
import com.fightingFish.user.bean.Anchor;
import com.fightingFish.user.bean.Gift;
import com.fightingFish.user.bean.User;
import com.fightingFish.user.service.AdministratorService;
import com.fightingFish.user.service.AnchorService;
import com.fightingFish.user.service.impl.AdministratorServiceImpl;
import com.fightingFish.user.service.impl.AnchorServiceImpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.util.List;

@WebServlet("/administrator")
public class AdministratorServlet extends HttpServlet {


    private AdministratorService asi = new AdministratorServiceImpl();
    private AnchorService as = new AnchorServiceImpl();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置字符集编码和返回类型
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        //获取参数
        String methodName = req.getParameter("methodName");
        try {
            Method method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this, req, resp);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    /**
     * 管理员登录
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void managerLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        //获取账号密码
        String account = req.getParameter("account");
        String pwd = req.getParameter("pwd");
        //对账号密码进行封装
        Administrator administrator = new Administrator(account, pwd);
        //调用service层方法进行登录
        if (asi.managerLogin(administrator).equals("登录成功")) {

//            调用方法加载页面
            getGiftAll(req, resp);
        }
        resp.getWriter().print(asi.managerLogin(administrator));
    }

    /**
     * 根据用户ID对用户进行管理
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void userManage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取用户ID
        int userId = 0;
        int state = 0;
        try {
            userId = Integer.parseInt(req.getParameter("userId"));
            state = Integer.parseInt(req.getParameter("state"));
        } catch (Exception e) {
        }
        String result = asi.userManage(userId, state);
        resp.getWriter().print(result);

    }

    /**
     * 方法概述:获取普通用户信息·主播用户信息 ·礼物信息
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void getGiftAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//      出适合当前页面
        Integer pageNo = 1;
        try {
            pageNo = Integer.parseInt(req.getParameter("pageNo"));
        } catch (Exception e) {
        }
        //        获取普通用户信息
        req.setAttribute("getUser", asi.getUserAll());


//        获取主播信息
        req.setAttribute("getAnchor", as.getAnchorAll());
//         获取礼物清单
        req.setAttribute("gift", asi.getGift());
//        获取总共送礼金额
        req.setAttribute("priceAll",asi.getPriceAll());
//       获取所有充值金额
        req.setAttribute("recharge",asi.rechargeAmount());
        RequestDispatcher rd = req.getRequestDispatcher(req.getContextPath() + "/jsp/management_system.jsp");
//        转发
        rd.forward(req, resp);
//        resp.sendRedirect(req.getContextPath()+"/jsp/management_system.jsp");
    }


    /**
     * 方法概述：修改礼物信息
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void updateGift(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取礼品id
        int giftId = Integer.parseInt(req.getParameter("giftId"));
//获取礼品名字
        String giftName = req.getParameter("giftName");
//        获取价格
        Double giftPrice = Double.valueOf(req.getParameter("giftPrice"));
//       获取礼品图片地址
        String giftImgUrl = req.getParameter("giftImgUrl");
        String url = "/images" + giftImgUrl.substring(giftImgUrl.lastIndexOf("/"));
//        System.out.println(url);

        List<Gift> update = asi.update(giftId, giftName, giftPrice, url);
//        放入域中
        if (update != null) {
            resp.getWriter().print("修改成功");
//          调用方法重新加载页面
//            getGiftAll(req, resp);

        } else {
            resp.getWriter().print("修改失败");
        }

    }

    /**
     * 方法概述： 新增礼物
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void giftAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//         String giftName, double giftPrice, String giftImgUrl
//                获取新增礼物的名称
        String giftName = req.getParameter("giftName");
//        获取新增礼物的价格
        double giftPrice = Double.valueOf(req.getParameter("giftPrice"));
//        获取新增礼物的图片名字
        String giftImgUrl ="/images/"+ req.getParameter("giftImgUrl");
        System.out.println(giftName +"   " + giftImgUrl+"   "+ giftPrice);
        asi.add(giftName, giftPrice, giftImgUrl);


        getGiftAll(req, resp);
    }

    /**
     * 方法概述： 修改礼物
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void giftRemove(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//         String giftName, double giftPrice, String giftImgUrl
//                获取删除礼物的Id
        int giftId = Integer.parseInt(req.getParameter("giftId"));
        asi.remove(giftId);
        getGiftAll(req, resp);
    }

    /**
     * 方法概述：查询用户
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void getAnchorUserId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//         String giftName, double giftPrice, String giftImgUrl
//                获取查询关键字

        String a = req.getParameter("number");

        if (!"".equals(a)){
            System.out.println(a);
            User user = new User();
//        查询主播
            int number = Integer.parseInt(a);
            user = as.getAnchorUserId(number);
            System.out.println(user.toString());
            resp.getWriter().print(user);
        }



    }
}
